var ibMarketPluginPermissions = {
	groups: $A(),
	/*------------------------------*/
	/* Constructor 					*/
	init: function()
	{
		Debug.write("Initializing itemForm.js");
		
		document.observe("dom:loaded", function(){
			ibMarketPluginPermissions.loadGroups();
			ibMarketPluginPermissions.loadForm();
		});
	},
	loadGroups: function()
	{
		$$(".groupID").each(function(group){
			var id = parseInt(group.identify().substr(8));
			this.groups.push(id);
		}.bind(this));
	},
	loadForm: function()
	{
		$('VIEW[-1]').observe('click', this.checkAllViewCheckbox);
		$('USE[-1]').observe('click', this.checkAllUseCheckbox);
		
		$('perm_view+').observe('click', this.checkAllViewButton);
		$('perm_view-').observe('click', this.uncheckAllViewButton);
		
		$('perm_use+').observe('click', this.checkAllUseButton);
		$('perm_use-').observe('click', this.uncheckAllUseButton);
		
		ibMarketPluginPermissions.groups.each(function(id){
			$('VIEW['+id+']').observe('click', this.checkedGroupView);
			$('USE['+id+']').observe('click', this.checkedGroupUse);
			$('perm_group+_'+id).observe('click', this.groupCheck);
			$('perm_group-_'+id).observe('click', this.groupUncheck);
		}.bind(this));
	},
	checkAllViewCheckbox: function()
	{
		if($('VIEW[-1]').checked)
		{
			ibMarketPluginPermissions.groups.each(function(id){
				$('VIEW['+id+']').checked = true;
			});
		}
	},
	checkAllUseCheckbox: function()
	{
		if($('USE[-1]').checked)
		{
			ibMarketPluginPermissions.groups.each(function(id){
				$('USE['+id+']').checked = true;
			});
		}
	},
	checkAllViewButton: function()
	{
		$('VIEW[-1]').checked = true;
		ibMarketPluginPermissions.groups.each(function(id){
			$('VIEW['+id+']').checked = true;
		});
	},
	uncheckAllViewButton: function()
	{
		$('VIEW[-1]').checked = false;
		ibMarketPluginPermissions.groups.each(function(id){
			$('VIEW['+id+']').checked = false;
		});
	},
	checkAllUseButton: function()
	{
		$('USE[-1]').checked = true;
		ibMarketPluginPermissions.groups.each(function(id){
			$('USE['+id+']').checked = true;
		});
	},
	uncheckAllUseButton: function()
	{
		$('USE[-1]').checked = false;
		ibMarketPluginPermissions.groups.each(function(id){
			$('USE['+id+']').checked = false;
		});
	},
	checkedGroupView: function(event)
	{
		if(!event.element().checked)
		{
			$('VIEW[-1]').checked = false;
		}
	},
	checkedGroupUse: function(event)
	{
		if(!event.element().checked)
		{
			$('USE[-1]').checked = false;
		}
	},
	groupCheck: function(event)
	{
		var id = parseInt(event.element().identify().substr(12));
		$('VIEW['+id+']').checked = true;
		$('USE['+id+']').checked = true;
	},
	groupUncheck: function(event)
	{
		var id = parseInt(event.element().identify().substr(12));
		$('VIEW['+id+']').checked = false;
		$('USE['+id+']').checked = false;
		$('VIEW[-1]').checked = false;
		$('USE[-1]').checked = false;
	}
}

ibMarketPluginPermissions.init();
